<template>
	<view class="container">
		<view class="title">请选择院校标签</view>
		<view class="sub-title">请选择2-5个标签 可随时调整</view>
		<view class="label-list">
			<view class="label-item" @tap="labelChange(item, index)" :class="{'label-item-a': item.labelIsSelected}" v-for="(item,index) in dataList" :key="index">{{item.labelName}}</view>
		</view>
		<view class="custom-box">
			<view class="custom-item" @tap="goCreateLabel">+自定义标签</view>
			<view class="label-item" :class="{'label-item-a': item.isSelected}" @tap="customChange(item, index)" v-for="(item,index) in customLabelList" :key="index">{{item.name}}</view>
		</view>
		<view class="submit-btn" @tap="doSubmit">选好了</view>
	</view>
</template>

<script>
	import { http, showToastMsg } from '../../utils'
	
	export default {
		data() {
			return {
				t: 0,
				dataList: [],
				labelIdSet: new Set(),
				labelNameSet: new Set(),
				customLabelList: [],
				labelNum: 0,
				userInfo: null,
				
			}
		},
		
		onLoad(options) {
			if (options.t) {
				this.t = options.t
			}
			if (options.n) {
				this.labelNum = parseInt(options.n)
			}
			this.userInfo = uni.getStorageSync('userInfo')
			this.customLabelList = uni.getStorageSync('customLabelList')
			for (let i = 0; i < this.customLabelList.length; i++) {
				if (this.customLabelList[i].isSelected) {
					this.labelNameSet.add(this.customLabelList[i].name)
				}
			}
			this.fetchData()
		},
		
		methods: {
			
			fetchData() {
				const that = this
				let dataList = uni.getStorageSync('dataList')
				if (dataList && dataList.length > 0) {
					that.dataList = dataList
					for (let i = 0; i < dataList.length; i++) {
						if (dataList[i].labelIsSelected) {
							that.labelIdSet.add(dataList[i].labelId)
						}
					}
				} else {
					if (that.userInfo.userType == 1) {
						http('/label/match').then(res => {
							if (res.code == 20000) {
								that.dataList = res.data
							}
						})
					} else {
						http('/label/show').then(res => {
							if (res.code == 20000) {
								that.dataList = res.data
							}
						})
					}
				}
			},
			
			doSubmit() {
				const that = this
				let nameList = [...that.labelNameSet]
				let idList = [...that.labelIdSet]
				let len = nameList.length + idList.length
				let totalLen = len + that.labelNum
				if (that.labelNum <= 0) {
					if (len < 2) {
						return showToastMsg('至少选择2个标签')
					}
				} else {
					if (len <= 0) {
						return showToastMsg('至少选择1个标签')
					}
				}
				if (totalLen > 5) {
					return showToastMsg('最多选择5个标签')
				}
				http('/label/create', {
					labelIds: idList,
					labelNames: nameList
				}).then(res => {
					if (res.code == 20000) {
						uni.removeStorageSync('customLabelList')
						uni.removeStorageSync('dataList')
						if (that.t == 1) {
							uni.redirectTo({
								url: '/pages/my_label/my_label'
							})
						} else if (that.t == 2) {
							uni.redirectTo({
								url: '/pages/teacher_index/teacher_index'
							})
						} else {
							uni.redirectTo({
								url: '/pages/index/index'
							})
						}
					}
				})
			},
			
			goCreateLabel() {
				const that = this
				uni.navigateTo({
					url: '/pages/create_label/create_label?t=' + that.t
				})
			},
			
			customChange(item, index) {
				if (this.labelNameSet.has(item.name)) {
					this.customLabelList[index].isSelected = false
					this.labelNameSet.delete(item.name)
				} else {
					this.customLabelList[index].isSelected = true
					this.labelNameSet.add(item.name)
				}
			},
			
			labelChange(item, index) {
				if (this.labelIdSet.has(item.labelId)) {
					this.dataList[index].labelIsSelected = false
					this.labelIdSet.delete(item.labelId)
				} else {
					this.dataList[index].labelIsSelected = true
					this.labelIdSet.add(item.labelId)
				}
				uni.setStorageSync('dataList', this.dataList)
			}
			
		}
	}
</script>

<style>
	.container {
		width: 750rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	
	.title {
		color: #333333;
		font-size: 48rpx;
		margin-top: 40rpx;
	}
	
	.sub-title {
		color: #999999;
		font-size: 30rpx;
		margin-top: 10rpx;
	}
	
	.label-list {
		width: 730rpx;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		flex-wrap: wrap;
		margin-top: 40rpx;
	}
	
	.label-item {
		margin-left: 20rpx;
		margin-top: 30rpx;
		border: 2rpx solid #cccccc;
		padding-left: 26rpx;
		padding-right: 26rpx;
		padding-top: 8rpx;
		padding-bottom: 8rpx;
		color: #999999;
		font-size: 30rpx;
		border-radius: 30rpx;
	}
	
	.label-item-a {
		color: #ffffff;
		background-color: #45d6b7;
		border: 2rpx solid #45d6b7;
	}
	
	.custom-box {
		width: 730rpx;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		flex-wrap: wrap;
	}
	
	.custom-item {
		padding-left: 26rpx;
		padding-right: 26rpx;
		padding-top: 8rpx;
		padding-bottom: 8rpx;
		color: #999999;
		font-size: 30rpx;
		border: 2rpx solid #cccccc;
		border-radius: 30rpx;
		margin-left: 20rpx;
		margin-top: 30rpx;
	}
	
	.submit-btn {
		width: 556rpx;
		height: 92rpx;
		background-color: #45d6b7;
		color: #ffffff;
		font-size: 36rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		border-radius: 46rpx;
		margin-top: 600rpx;
	}
</style>
